﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Model
{
    public class BaseResponse
    {
        /// <summary>
        /// 请求响应实体类
        /// </summary>
        public BaseResponse()
        {
            code = ResponseCode.OK;
            Message = "处理成功";
            timespan = DateTime.Now;
        }
        /// <summary>
        /// 响应代码
        /// </summary>
        [JsonProperty("code")]
        public ResponseCode code { get; set; }
        /// <summary>
        /// 响应消息内容
        /// </summary>
        [JsonProperty("message")]
        public string Message { get; set; }
        /// <summary>
        /// 返回的响应数据
        /// </summary>
        [JsonProperty("data")]
        public object Data { get; set; }
        [JsonProperty("total")]
        public int Total { get; set; }
        [JsonProperty("timespan")]
        public DateTime timespan { get; set; }
        /// <summary>
        /// 设置响应状态为成功
        /// </summary>
        /// <param name="message"></param>
        public void SetSuccess(string message = "success")
        {
            code = ResponseCode.OK;
            Message = message;
        }
        /// <summary>
        /// 设置响应状态为失败
        /// </summary>
        /// <param name="message"></param>
        public void SetFailed(string message = "fail")
        {
            Message = message;
            code = ResponseCode.NOEXPECT;
        }
        public void SetSelfCodeAndMessage(string message, ResponseCode code, object data = null)
        {
            Message = message;
            this.code = code;
            Data = data;
        }
        public void SetSelfCode(string message, ResponseCode code)
        {
            Message = message;
            this.code = code;
        }
        /// <summary>
        /// 设置响应状态为未知资源
        /// </summary>
        /// <param name="message"></param>
        public void SetNotFound(string message = "unknown source")
        {
            Message = message;
            code = ResponseCode.UNKNOW;
        }

        /// <summary>
        /// 设置响应状态为无权限
        /// </summary>
        /// <param name="message"></param>
        public void SetNoPermission(string message = "no auth")
        {
            Message = message;
            code = ResponseCode.UNAUTH;
        }
    }
}
